﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DesktopGarden.Classes;

namespace DesktopGarden.Controls
{
    /// <summary>
    /// Interaction logic for GardenViewer.xaml
    /// </summary>
    public partial class GardenViewer : UserControl
    {

        bool _hasChanges = false;

        public bool HasChanges
        {
            get { return _hasChanges; }
            set{
                if(value)
                {
                    if (ChangeMade != null)
                        ChangeMade(this, null);
                }
                _hasChanges = value;
            }
        }

        List<FlowerTile> SelectedFlowerTiles;

        List<FlowerTile> FlowerTiles;

        List<FlowerTile> TempAddedFlower;

        List<FlowerTile> TempDeletedFlower;

        public EventHandler ChangeMade { get; set; }

        public EventHandler Saved { get; set; }

        public EventHandler Cancelled { get; set; }



        public Garden Garden { get; set; }

        public GardenViewer()
        {
            InitializeComponent();
        }

        public void LoadGarden(Garden garden)
        {
            App.CurrentGarden = garden;
            RefreshGarden();
            HasChanges = false;
        }


        public void RefreshGarden()
        {
            ugMain.Children.Clear();
            ugMain.Rows = App.CurrentGarden.Rows;
            ugMain.Columns = App.CurrentGarden.Columns;
            ugMain.Width = 30 * App.CurrentGarden.Columns;
            ugMain.Height = 30 * App.CurrentGarden.Rows;


            SelectedFlowerTiles = new List<FlowerTile>();
            FlowerTiles = new List<FlowerTile>();
            TempAddedFlower = new List<FlowerTile>();
            TempDeletedFlower = new List<FlowerTile>();


            for (int r = 1; r <= App.CurrentGarden.Rows; r++)
            {
                for (int c = 1; c <= App.CurrentGarden.Columns; c++)
                {
                    var flowersInTile = (from f in App.CurrentGarden.Flowers
                                         where f.HasFlowersInTiles(r,c)
                                         select f).ToList();

                    FlowerTile ft = new FlowerTile(flowersInTile);
                    ft.Row = r;
                    ft.Column = c;
                    ft.SelectedEvent = (x, y) => SelectedFlowerTiles.Add(ft);
                    ft.UnSelectedEvent = (x, y) => SelectedFlowerTiles.Remove(ft);
                    ft.FlowerAdded = new EventHandler(FlowerAdd);
                    ft.FlowerDeleted = new EventHandler(FlowerDelete);
                    FlowerTiles.Add(ft);
                    ugMain.Children.Add(ft);

                }
            }
        }

        public void ClearSelection()
        {
            while (SelectedFlowerTiles.Count != 0)
            {
                SelectedFlowerTiles[0].Clear();
            }

            SelectedFlowerTiles.Clear();
        }

        internal void SelectAll(FlowerInstance fl)
        {
            ClearSelection();
            var flowerTiles = from f in FlowerTiles
                              where f.Flowers.Contains(fl)
                              select f;
            foreach(var flower in flowerTiles)
                flower.Selected = true;
        }

        public void FlowerAdd(object sender, EventArgs e)
        {
            FlowerTile f = sender as FlowerTile;
            TempAddedFlower.Add(f);
            HasChanges = true;

        }

        public void FlowerDelete(object sender, EventArgs e)
        {
            FlowerTile f = sender as FlowerTile; 
            TempDeletedFlower.Add(f);
            HasChanges = true;
        }

        public void CancelAction()
        {
            if (App.CurrentMode == App.Mode.Plant)
            {
                foreach (FlowerTile f in TempAddedFlower)
                {
                    f.CancelAddedFlowers();
                }

                TempAddedFlower.Clear();
            }
            else if (App.CurrentMode == App.Mode.Delete)
            {
                foreach (FlowerTile f in TempDeletedFlower)
                {
                    f.CancelDeletedFlowers();
                }

                TempDeletedFlower.Clear();
            }

            if(Cancelled != null)
                Cancelled(this, null);

            HasChanges = false;
        }

        public void SaveAction()
        {
            if (App.CurrentMode == App.Mode.Plant)
            {
                foreach (FlowerTile f in TempAddedFlower)
                {
                    f.SaveAddedFlowers();
                }

                TempAddedFlower.Clear();
            }
            else if (App.CurrentMode == App.Mode.Delete)
            {
                foreach (FlowerTile f in TempDeletedFlower)
                {
                    f.SaveDeletedFlowers();
                }

                TempDeletedFlower.Clear();
            }

            if(Saved != null)
                Saved(this, null);

            App.CurrentGarden.Save();

            HasChanges = false;
            
        }

        public void RefreshFlower(FlowerInstance flower)
        {
            var flowerTiles = (from f in FlowerTiles
                               where f.HasFlower(flower)
                               select f);
            foreach (var f in flowerTiles)
            {
                f.Refresh();
            }
        }
    }
}
